package com.net.xpay.web.controller.common;

import com.google.common.base.Strings;
import com.net.common.model.ResultData;
import com.net.xpay.common.domain.SiteConfig;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.UserProfile;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.manager.UserProfileManager;
import com.net.xpay.common.manager.partner.PartnerManager;
import com.net.xpay.common.service.SiteConfigService;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Slf4j
@RestController
@RequestMapping("/common/site-config")
@Api(tags = "公共/配置网站属性", description = "公共/配置网站属性 net")
public class SiteConfigController {
    @Autowired
    private SiteConfigService siteConfigService;

    @Autowired
    private PartnerManager partnerManager;

    @Autowired
    private UserProfileManager userProfileManager;

    @ApiOperation(value = "获取网站配置信息")
    @GetMapping
    public ResultData<SiteConfig> getWebSiteConfig(@ApiIgnore @RequestUser User user) {
        SiteConfig siteConfigInformation = siteConfigService.findSiteConfig();
        if (siteConfigInformation == null) {
            return new ResultData<>(siteConfigInformation);
        }
        if (user == null) {
            return new ResultData<>(siteConfigInformation);
        }
        UserProfile userProfile = userProfileManager.getById(user.getId());
        if (Boolean.TRUE.equals(userProfile.getNoWechatQrCode())) {
            //不显示客服微信二维码
            siteConfigInformation.setWechatQrCode(null);
            return new ResultData<>(siteConfigInformation);
        }

        if (user.getPartnerId() != null) {
            Partner partner = partnerManager.getById(user.getPartnerId());
            if (!Strings.isNullOrEmpty(partner.getWechatQrCode())) {
                siteConfigInformation.setWechatQrCode(partner.getWechatQrCode());
            }
        }
        return new ResultData<>(siteConfigInformation);
    }

}
